package kr.hyunist.androidcomplete01saeildamil.ch12;

import java.util.*;

import kr.hyunist.androidcomplete01saeildamil.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;

public class SpinnerListViewTest extends Activity {
	String[] rank = { "1 ~ 5", "6 ~ 10", "11 ~ 15" };
	String[] mName = { "인터스텔라", "나의사랑나의신부", "다크나이트라이즈", "콘스탄틴", "별이되어라",
			"별이되뿔까나?", "무한도전", "마녀사냥", "SNL코리아", "런닝맨", "개그콘서트", "미생", "타짜", "백설공주", "나머지ETC.." };
	ArrayList<MyRank> arList;
	MyRank mr;
	RankAdapter lAdapter;
	ListView list;
	boolean mInit = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spinnerlistview);

		ArrayAdapter<String> sAdapter = new ArrayAdapter<String>(
				SpinnerListViewTest.this,
				android.R.layout.simple_spinner_dropdown_item, rank);

		Spinner spin = (Spinner) findViewById(R.id.spinlistviewsp1);
		spin.setAdapter(sAdapter);

		arList = new ArrayList<MyRank>();
		/*for(int i = 0; i < mName.length; i++){
			mr = new MyRank(mName[i], i + 1);
			arList.add(mr);
		}*/
		
		lAdapter = new RankAdapter(arList, SpinnerListViewTest.this);

		list = (ListView) findViewById(R.id.spinlistviewlist1);
		//list.setAdapter(lAdapter);

		spin.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				
				if (mInit == false) {
					mInit = true;
					return;
				}

				//포지션에 따른 setAdapter
				if (position == 0) {
					if(arList.size() != 0){
						arList.clear();
					}
					for(int i = 0; i < 5; i++){
						mr = new MyRank(mName[i], i + 1);
						arList.add(mr);
					}
					list.setAdapter(lAdapter);
					lAdapter.notifyDataSetChanged();
					
				} else if(position == 1){
					if(arList.size() != 0){
						arList.clear();
					}
					for(int i = 5; i < 10; i++){
						mr = new MyRank(mName[i], i + 1);
						arList.add(mr);
					}
					list.setAdapter(lAdapter);
					lAdapter.notifyDataSetChanged();
					
				} else if(position == 2){
					if(arList.size() != 0){
						arList.clear();
					}
					for(int i = 10; i < 15; i++){
						mr = new MyRank(mName[i], i + 1);
						arList.add(mr);
					}
					list.setAdapter(lAdapter);
					lAdapter.notifyDataSetChanged();
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}

		});

	}
}

class MyRank {
	String name;
	int rankNum;

	public MyRank(String name, int rankNum) {
		super();
		this.name = name;
		this.rankNum = rankNum;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getRankNum() {
		return rankNum;
	}

	public void setRankNum(int rankNum) {
		this.rankNum = rankNum;
	}

}

class RankAdapter extends BaseAdapter {
	private ArrayList<MyRank> arRank;
	private Context mContext;
	private LayoutInflater inflater;

	public RankAdapter(ArrayList<MyRank> arList, Context context) {
		this.arRank = arList;
		this.mContext = context;
		inflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return arRank.size();
	}

	@Override
	public Object getItem(int position) {
		return arRank.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.spinlistcontent, parent,
					false);
		}

		TextView rank = (TextView) convertView
				.findViewById(R.id.spinlisttesttv1);
		TextView title = (TextView) convertView
				.findViewById(R.id.spinlisttesttv2);
		
		rank.setText(String.valueOf(arRank.get(position).getRankNum()));
		title.setText(arRank.get(position).getName());
		
		return convertView;
	}

}
